//
//  Person.m
//  BaseToolsKit_Example
//
//  Created by zyming on 2022/8/25.
//  Copyright © 2022 zyming. All rights reserved.
//

#import "Person.h"
#import "JQFMDB.h"
#define GDDB [JQFMDB shareDatabase:@"Guider.sqlite"]
#define GDDBTable_Account @"AccountModel"
//#define GDDBTable_SelectedAccount @"SelectedAccountModel"
@implementation Person

+ (void)saveAccountInfo:(Person *)account{

    //是否存在表,不存在则创建
    if (![GDDB jq_isExistTable:GDDBTable_Account]) {
        BOOL createSuccess = [GDDB jq_createTable:GDDBTable_Account dicOrModel:[Person class]];
        NSLog(@"%@表创建会否成功:%d",GDDBTable_Account,createSuccess);
    }

    //查询当前表是否存在改用户
    NSArray <Person *>*array = [GDDB jq_lookupTable:GDDBTable_Account dicOrModel:[Person class] whereFormat:[NSString stringWithFormat:@"where accountId = '%@'",account.accountId]];

    //存在则更新,不存在则新增
    if (array.count > 0) {
        [GDDB jq_inDatabase:^{
            BOOL isSuccess = [GDDB jq_updateTable:GDDBTable_Account dicOrModel:account whereFormat:[NSString stringWithFormat:@"where accountId = '%@'",account.accountId]];
            NSLog(@"用户AccountId:%@ 是否刷新成功:%d",account.accountId,isSuccess);
        }];
    }else{
        [GDDB jq_inDatabase:^{
//            BOOL deleteSuccess = [GDDB jq_deleteAllDataFromTable:GDDBTable_Account];
//            if (deleteSuccess) {
                BOOL isSuccess = [GDDB jq_insertTable:GDDBTable_Account dicOrModel:account];
                NSLog(@"用户AccountId:%@ 是否存储成功:%d",account.accountId,isSuccess);
//            }
        }];
    }
}
+ (Person *)getAccountInfo{
        //查询当前表是否存在改用户
        NSArray <Person *>*array = [GDDB jq_lookupTable:GDDBTable_Account dicOrModel:[Person class] whereFormat:nil];
    if (array.count > 0) {
        return array.lastObject;
    }else{
        return [Person new];
    }
}

- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
    

}
@end
